import java.awt.Color;
import java.awt.Graphics;
import java.awt.Panel;
import java.awt.Rectangle;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import org.epistasis.gui.ReadOnlyTableModel;
import org.jdesktop.swingx.MultiSplitLayout;
import org.jdesktop.swingx.MultiSplitLayout.Divider;
import org.jdesktop.swingx.MultiSplitLayout.Node;
import org.jdesktop.swingx.MultiSplitLayout.Split;
import org.jdesktop.swingx.MultiSplitPane;

public class MultiTable extends MultiSplitPane{

	ArrayList<JTable> tables = new ArrayList<JTable>();
	private Split root;
	private List<Node> l = new LinkedList<Node>();

	public MultiTable(){
		super();
		setDividerPainter(new DividerPainter() {
			
			@Override
			public void paint(Graphics g, Divider divider) {
				Rectangle bounds = divider.getBounds();
				int yMin = bounds.y;
				int yMax = bounds.y + bounds.height;
				int xMin = bounds.x;
				int xMax = bounds.x + bounds.width;
				
				int y1 = (yMin * 2 + yMax) / 3;
				int y2 = (yMin + yMax * 2) / 3;
				
				int x1 = xMin + 5;
				int x2 = xMax - 5;
				
				g.setColor(Color.GRAY);
				
				g.drawLine(x1, y1, x2, y1);
				g.drawLine(x1, y2, x2, y2);
			}
		});
		root = new Split();
		root.setRowLayout(false);
		setModel(root);
	}

	public void addRow(Vector<String> row, int table){
		if(tables.size() <= table){
			addTable(table);
		}
		((ReadOnlyTableModel)tables.get(table).getModel()).addRow(row);
	}

	private void addTable(int table) {
		while(tables.size() <= table){
			ReadOnlyTableModel model = new ReadOnlyTableModel();
			model.addColumn("A");
			model.addColumn("B");
			model.addColumn("C");
			model.addColumn("D");
			JTable newTable = new JTable(model);
			tables.add(newTable);
			if(l.size() > 0){
				l.add(new Divider());
				newTable.setTableHeader(null);
			}
			String name = "table" + (tables.size() - 1);
			l.add(new MultiSplitLayout.Leaf(name));
			add(new JScrollPane(newTable), name);
		}
		
		root.setChildren(l);
	}

	public static void main(String[] args){
		ArrayList<String> dictionary = new ArrayList<String>();
		Scanner s;
		try {
			s = new Scanner(new File("C:\\users\\ramos\\Documents\\dictionary.txt"));
		} catch (FileNotFoundException e) {
			return;
		}
		while(s.hasNext())
			dictionary.add(s.nextLine());
		JFrame f = new JFrame();
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		MultiTable t = new MultiTable();
		f.add(t);
		for(int i = 0; i < 100; i++){
			Vector<String> v = new Vector<String>();
			for(int j = 0; j < 4; j++){
				v.add(dictionary.get((int) (Math.random() * dictionary.size())));
			}
			t.addRow(v, (int) (Math.random() * 6));
		}
		f.setSize(100, 100);
		f.setVisible(true);
	}
}
